<?php
include_once(APPLICATION_PATH."/../library/WxPayPubHelper/WxPayPubHelper.php");
class Application_Model_WXApi
{
  function getOpenId($code) {
    if (empty($code))
		return null;
    $appid="wx13b0ce87f8202ad4";
    $appsecret="a3f553d78d3859a04491c59816b53772";
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
    $data = curl_exec($curl);
    curl_close($curl);
	
    $arr = explode('"',$data);

    if (count($arr) > 14)
      return $arr[13]; // OPENID
    return null;
  }

  function genereateWXPayParams($orderid, $ordercode, $price,$inserttime) {
  	$jsApi = new JsApi_pub();

	//=========步骤2：使用统一支付接口，获取prepay_id============
	//使用统一支付接口
	$unifiedOrder = new UnifiedOrder_pub();
	$auth = new Application_Model_Auth();
	$userid = $auth->getUserId();
	$openIdNamespace = new Zend_Session_Namespace('open');
	$openid = $openIdNamespace->openId;
	//设置统一支付接口参数
	//设置必填参数
	//appid已填,商户无需重复填写
	//mch_id已填,商户无需重复填写
	//noncestr已填,商户无需重复填写
	//spbill_create_ip已填,商户无需重复填写
	//sign已填,商户无需重复填写
	$unifiedOrder->setParameter("openid","$openid");//商品描述
	$unifiedOrder->setParameter("body","蔬菜水果生鲜肉禽");//商品描述
	//自定义订单号，此处仅作举例
	$timeStamp = time();
	//$out_trade_no = WxPayConf_pub::APPID."$timeStamp";
	$out_trade_no = $ordercode;
	$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
	$unifiedOrder->setParameter("total_fee", $price * 100);//总金额
	$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
	$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
	//非必填参数，商户可根据实际情况选填
	//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号  
	//$unifiedOrder->setParameter("device_info","XXXX");//设备号 
	//$unifiedOrder->setParameter("attach","XXXX");//附加数据
	$time_insert = strtotime($inserttime);
	$startTime = date('YmdHis',$time_insert);
	$endTime = date('YmdHis',$time_insert+900); 
	$unifiedOrder->setParameter("time_start","$startTime");//交易起始时间
	$unifiedOrder->setParameter("time_expire","$endTime");//交易结束时间 
	//$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 
	//$unifiedOrder->setParameter("openid","XXXX");//用户标识
	//$unifiedOrder->setParameter("product_id","XXXX");//商品ID

	$prepay_id = $unifiedOrder->getPrepayId();
	//=========步骤3：使用jsapi调起支付============
	$jsApi->setPrepayId($prepay_id);

	$obj = $jsApi->getParameters();
	return json_decode($obj);
  }
  
  function genereateTopupWXPayParams($transactionId,$price,$inserttime) {
  	$jsApi = new JsApi_pub();

	//=========步骤2：使用统一支付接口，获取prepay_id============
	$unifiedOrder = new UnifiedOrder_pub();
	$openIdNamespace = new Zend_Session_Namespace('open');
	$openid = $openIdNamespace->openId;
	$unifiedOrder->setParameter("openid","$openid");//商品描述
	$unifiedOrder->setParameter("body","鲜店余额充值");//商品描述
	$out_trade_no = $transactionId;
	$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
	$unifiedOrder->setParameter("total_fee", $price * 100);//总金额
	$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
	$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
//	$time_insert = strtotime($inserttime);
//	$startTime = date('YmdHis',$time_insert);
//	$endTime = date('YmdHis',$time_insert+900); 
//	$unifiedOrder->setParameter("time_start","$startTime");//交易起始时间
//	$unifiedOrder->setParameter("time_expire","$endTime");//交易结束时间 
	$prepay_id = $unifiedOrder->getPrepayId();
	//=========步骤3：使用jsapi调起支付============
	$jsApi->setPrepayId($prepay_id);

	$obj = $jsApi->getParameters();
	return json_decode($obj);
  }
}

